package com.mcptt.db.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.res.Resource;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.DbConst;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

public class UserInfo extends AddrInfo implements Serializable
{   
//	public static final int McType_Ptt = 1;
//	public static final int McType_Video = 2;
//	public static final int McType_Data = 4;
//	public static final int McType_All = McType_Ptt + McType_Video + McType_Data;
//	public static final int McType_None = 0;

//	private String mcpttId = null;
//	private String mcvideoId = null;
//	private String mcdataId = null;
	private int attr = 0;
	private int onlineState = 0;
	private String ip = "";
	private String groupId = null;

	//	private boolean isInCall = false;;
	
	public static UserInfo getSysInfo()
	{
		String sysName = Resource.getString("sys_name");
		UserInfo info = new UserInfo(UserType_Sys,sysName,UserInfo.Sys_Num,"3000");
		return info;
	}

	public boolean isDefaultInfo()
	{
		return this.type == UserType_Default && 
				name.equals("");
	}


	public UserInfo() {
		this.mcId = "";
		this.type = UserType_User;
		this.name = "";
	}
	/**
	 * 
	 * @param usertype 用户类型
	 * @param uId      PUID
	 * @param name     名称
	 * @param num      MDN
	 * @param image    图片编号
	 */
	public UserInfo(int userType,String name,String num,String groupId)
	{
		super(userType,name,num,0);
		this.groupId = groupId;
		this.attr = 0;
	}
	
	


	public UserInfo(Cursor c)
	{
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}

	public UserInfo(String mcId) {
		this.mcId = mcId;
		this.type = UserType_Default;
		this.name = mcId;
	}
	
	public UserInfo(String mcId,String name) {
		this.mcId = mcId;
		this.type = UserType_User;
		this.name = name;
	}
	
	
	public void setAttr(int attr) 
	{
		this.attr = attr;
	}



	/**
	 * Pack the object content value to store
	 * 
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Id, mcId);
		cv.put(DbConst.COL_Type,type);
		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_Attr, attr);
		cv.put(DbConst.COL_State, onlineState);
		cv.put(DbConst.COL_GroupId, groupId);

		return cv;
	}
	
	public static ContentValues getUpdateContentValues(String name) 
	{
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Name, name);
		return cv;
	}
	
	public ContentValues getUpdateContentValues(int mcType) {
		ContentValues cv = new ContentValues();
//		cv.put(DbConst.COL_McId, mcId);
//		cv.put(DbConst.COL_UserType,userType);
//		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_Attr, attr);
		cv.put(DbConst.COL_GroupId, groupId);

		return cv;
	}

	public final void createFromContentValue(ContentValues args) {

//		Log.debug(this.getClass(), "createFromContentValue");
		String tmp_s;

		tmp_s = args.getAsString(DbConst.COL_Id);
		if(tmp_s != null) {
			mcId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			type = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Attr);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				attr = value;
			}
		}
		tmp_s = args.getAsString(DbConst.COL_GroupId);
		if(tmp_s != null) {
			groupId = tmp_s;
		}

		if(args.getAsString(DbConst.COL_State) != null)
		{
			int tmp_int = args.getAsInteger(DbConst.COL_State);
//			Log.debug(this.getClass(),  "State = "+tmp_s);
			if(tmp_int != 0){
				onlineState = tmp_int;
			}
		}
		if(args.getAsString(DbConst.COL_Ip) != null)
		{
			String tmp = args.getAsString(DbConst.COL_Ip);
//			Log.d("lq", "ip = "+tmp);
			if(ip != null){
				ip = tmp;
			}
		}
	}

	public int getUserType() {
		return type;
	}
	
//	public boolean isUser() {
//		if(!isDac()
//				&& isCamera())
//		{
//			return true;
//		}
//		return false;
//	}
	
	public void setUserType(int userType) {
		this.type = userType;
	}

	//	public String getuId()
	//	{
	//		return uId;
	//	}
	//	public void setuId(String uId)
	//	{
	//		this.uId = uId;
	//	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public String getMcId() {
		return mcId;
	}


	public boolean isAffiliation()
	{
		if(attr ==1)
		{
			return true;
		}
		return false;
	}

	public void setIsAffiliation(boolean isAffiliatio)
	{
		if(isAffiliatio)
		{
			attr =1;
		}
		else
		{
			attr = 0;
		}
	}
	
	public void setOnlineState(int state){
		onlineState = state;
	}
	
	public int getOnlineState(){
		return onlineState;
	}
	public String getIp() {
		return ip;
	}
	public String getGroupId() {
		return groupId;
	}
	public CameraInfo toCameraInfo()
	{
		return new CameraInfo(name,mcId);
	}
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userType = ").append(type).append(",");
		strBuf.append("mcId = ").append(mcId).append(",");
		strBuf.append("name = ").append(name).append(",");
		strBuf.append("attr = ").append(attr).append(",");
		strBuf.append("groupId = ").append(groupId).append(",");
		strBuf.append("onlineState = ").append(onlineState).append(".");


		return strBuf.toString();
	}

	public static String convertDscpNum(String num)
	{
		//		if(num.startsWith(Default_CountryCode))
		//		{
		//			return num.substring(Default_CountryCode.length() , num.length());
		//		}
		return num;
	}
	
//	public static List<UserInfo> filterMyself(McIdInfo mcIdInfo,List<UserInfo> oldList)
//	{
//		List<UserInfo> list = new Vector<UserInfo>();
//		if(oldList == null
//				|| oldList.size() <= 0)
//		{
//			return list;
//		}
//		if(mcIdInfo == null
//				|| mcIdInfo.getMcpttId() == null)
//		{
//			return oldList;
//		}
//		for(Iterator<UserInfo> itor = oldList.iterator();itor.hasNext();)
//		{
//			UserInfo info = itor.next();
//			if(info != null
//					&& !info.isSameOne(mcIdInfo))
//			{
//				list.add(info);
//			}
//		}
//		return list;
//	}
	
	public static List<UserInfo> filterOnlyOtherUser(McIdInfo mcIdInfo,List<UserInfo> list){
		List<UserInfo> userList = new ArrayList<UserInfo>();
		if(list == null
				|| list.size()<=0)
		{
			return userList;
		}
		for(int i = 0 ; i < list.size(); i++){
			UserInfo info = list.get(i);
			if(info != null
					&& !info.isSameOne(mcIdInfo)
					&& UserInfo.UserType_User == info.getUserType())
			{
				userList.add(list.get(i));
			}
		}
		
		return userList;
	}

//	private final static String Key_Mcptt = "3gpp-service.ims.icsi.mcptt";
//	private final static String Key_Mcvideo = "3gpp-service.ims.icsi.mcvideo";
//	private final static String Key_Mcdata = "3gpp-service.ims.icsi.mcdata";
//	public static int getMcTypeFromMsg(String msg)
//	{
//		if(msg.contains(Key_Mcptt))
//		{
//			return UserInfo.McType_Ptt;
//		}
//		else if(msg.contains(Key_Mcvideo))
//		{
//			return UserInfo.McType_Video;
//		}
//		else if(msg.contains(Key_Mcdata))
//		{
//			return UserInfo.McType_Data;
//		}
//		return 0;
//	}
}
